﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using HotelManager.Model;
using HotelManager.Core;

namespace HotelManager.DataAccess
{
    class PhongAccess
    {
        dbConnection db = new dbConnection();
        public DataTable laytoanbophong()
        {
            return db.executeSelectProcedureQueryNonParameter("selectPhong");
        }

        // Tìm kiếm theo mã phòng
        public DataView timkiemMaphong(string maphong)
        {
            DataView dataView = laytoanbophong().DefaultView;
            dataView.RowFilter = string.Format("MaPhong like '%{0}%'", maphong);
            return dataView;
        }
        // tìm kiếm theo số phòng
        public DataView timkiemSophong(string sophong)
        {
            DataView dataView = laytoanbophong().DefaultView;
            dataView.RowFilter = string.Format("SoPhong like '%{0}%'", sophong);
            return dataView;
        }
        // tìm kiếm phòng theo mã và trả về 1 bảng
        public DataTable timPhongtheoma(string maphong)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@MaPhong", SqlDbType.Char, 10);
            ma.Value = maphong;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("timkiemPhong", listParam);
        }

        public int suaPhong(Phong phong)
        {
            if ((timPhongtheoma(phong.MaPhong1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter maphong = new SqlParameter("@MaPhong", SqlDbType.Char, 10);
                maphong.Value = phong.MaPhong1;
                listParam[0] = maphong;
                listParam[1] = new SqlParameter("@LoaiPhong", phong.LoaiPhong1);
                listParam[2] = new SqlParameter("@SoPhong", phong.SoPhong1);
                listParam[3] = new SqlParameter("@GiaPhong", phong.GiaPhong1);    
                listParam[4] = new SqlParameter("@SDT", phong.MaPhong1);
                db.executeUpdateProcedureQuery("updatePhong", listParam);
                return 1;
            }
        }

        public int themPhong(Phong phong)
        {
            if ((timPhongtheoma(phong.MaPhong1).Rows.Count > 0))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter maphong = new SqlParameter("@MaPhong", SqlDbType.Char, 10);
                maphong.Value = phong.MaPhong1;
                listParam[0] = maphong;
                listParam[1] = new SqlParameter("@LoaiPhong", phong.LoaiPhong1);
                listParam[2] = new SqlParameter("@SoPhong", phong.SoPhong1);
                listParam[3] = new SqlParameter("@GiaPhong", phong.GiaPhong1);
                listParam[4] = new SqlParameter("@SDT", phong.MaPhong1);
                db.executeInsertProcedureQuery("insertPhong", listParam);
                return 1;
            }
        }

        public int xoaPhong(Phong phong)
        {
            if ((timPhongtheoma(phong.MaPhong1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                listParam[0] = new SqlParameter("@MaPhong", phong.MaPhong1);
                db.executeDeleteProcedureQuery("deletePhong", listParam);
                return 1;
            }
        }

    }
}
